/**
 * 审批页面
 * author:yinht
 * date:2017年6月2日19:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var iscroll = require("iscroll");
	var pending = {
		/**
		 * bindEvent 页面事件
		 */
		type: "", //判断页面显示隐藏
		supertype: "", //加载接口用的
		currentPage: 1,
		pageCount: 10,
		loadedAllFlag: false,
		bindEvent: function() {
			//返回
			$(".back").unbind().on("click", function() {
				window.localStorage.setItem("supertype", "");
				window.location.href = "#home";
			});
			$(".mem_cont").unbind().on("click", function() {
				$(".mem_cont").removeClass("actived_fo");
				$(this).addClass("actived_fo");
				$(".public").hide();
				$($(this).attr("datatype")).show();
				$("#willpend").html("");
				pending.currentPage = 1;
				pending.type = $(this).attr("type");
				pending.supertype = $(this).attr("supertype");
				pending.sendParam($(this).attr("supertype"));
				console.log(pending.type);
				window.localStorage.setItem("supertype", pending.supertype);
				window.localStorage.setItem("type", pending.type);
			});
			$(".input_sea").unbind().on("click", function(){
				window.location.href = "#/index/search";
			});
			//搜索
			
			$(document).keypress(function(e) {

				if(e.keyCode == 13) {
					$("#menbers").html("");
					pending.currentPage = 1;
					pending.loadedAllFlag = false;
					pending.sendParam(pending.supertype);
				}
			});
			
			$(".searchkey").unbind().on("click", function() {
				$("#menbers").html("");
				pending.currentPage = 1;
				pending.loadedAllFlag = false;
				pending.sendParam(pending.supertype);
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "left"
				});
			});
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			Path.listen();
		},

		/**
		 * 页面数据加载
		 */
		sendParam: function(type) {
			var searchKey = $(".pending_searchInput").val();
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"type": type,
				"pk_user": pk_user,
				"currentPage": pending.currentPage,
				"pageCount": pending.pageCount,
				"searchKey": searchKey
			}
			common.loadData({
				url: "/service/RRTPubServlet?method=mbrlist4approval", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: pending.reviewMemberCallBack, //回调函数
				dataSource: "reviewMember",
				cache: false
			});
		},
		loadData: function(currentPage) {
			//
			window.localStorage.setItem("type", "2");
			var currentPage;
			pending.currentPage = currentPage;
			var supertype;
			supertype = window.localStorage.getItem("supertype");
			if(!supertype || (supertype == "")) {
				supertype = "0";
			}
			pending.supertype = supertype;
			$(".mem_cont").removeClass("actived_fo");
			$(".mem_cont").each(function() {
				if($(this).attr("supertype") == supertype) {
					$(this).addClass("actived_fo");
				}

			});
			pending.sendParam(pending.supertype);
			console.log(pending.supertype);

		},
		reviewMemberCallBack: function(json) {
			if(pending.currentPage==1){
				$("#willpend").html("");
			}
			console.log(JSON.stringify(json));
			$(".pages1").text(json.data.page1);
			$(".pages2").text(json.data.page2);
			if(json.status == "0") {
				if(json.data && json.data.list && json.data.list.length > 0) {
					//展示页面
					var html = "";
					if(json.data.type == "0") {
						for(var i = 0; i < json.data.list.length; i++) {
							html += "<div class='men_mana pending public tiaozhuan prend_sub' listtype='"+json.data.list[i].listtype+"' pk_member='" +
								json.data.list[i].pk_member +
								"'>" +
								"	<div class='sp_img lf'>" +
								' <img src="images/' + json.data.list[i].besystem + '.png" alt="">' +
								"	</div>" +
								"	<div class='sp_item rf'>" +
								"	<div class='h_top'>" +
								"	<span class='lf'><b>" + json.data.list[i].name +
								"	</b>-<b>" + json.data.list[i].currentLevel +
								"	</b></span>" +
								"	<span class='rf mana_time'>" + json.data.list[i].createTime + "</span>" +
								"	</div>" +
								"	<div class='h_bottom'>" +
								"	<span class='lf'><b>对接人</b> : <b>" + json.data.list[i].docker + "</b></span>" +
								"	</div>" +
								"	</div>" +
								"	</div>";
						}
					} else if(json.data.type == "1") {
						for(var i = 0; i < json.data.list.length; i++) {
							if(json.data.list[i].ApprovalStatus == "已通过"){
								html += '<div class="men_mana pending reject_sub public tiaozhuan" listtype="'+json.data.list[i].listtype+'"  pk_member="' 
								+ json.data.list[i].pk_member +
								'">'+
								'<div class="sp_img lf">' +
								' <img src="images/' + json.data.list[i].besystem + '.png" alt="">' +
								'</div><div class="sp_item rf">' +
								'<div class="h_top">' +
								'<span class="lf"><b>' + json.data.list[i].name + '</b>-<b>' + json.data.list[i].currentLevel + '</b></span>' +
								'<span class="rf mana_time">' + json.data.list[i].createTime + '</span>' +
								'</div><div class="h_bottom">' +
								'<span class="lf"><b>对接人</b> : <b>' + json.data.list[i].docker + '</b></span>' +
								'<span class="rf tel passthis">'+json.data.list[i].ApprovalStatus+'</span>' +
								'</div></div></div>';
							}else if(json.data.list[i].ApprovalStatus == "已驳回"){
									html += '<div class="men_mana pending reject_sub public tiaozhuan" listtype="'+json.data.list[i].listtype+'"  pk_member="' 
								+ json.data.list[i].pk_member +
								'">'+
								'<div class="sp_img lf">' +
								' <img src="images/' + json.data.list[i].besystem + '.png" alt="">' +
								'</div><div class="sp_item rf">' +
								'<div class="h_top">' +
								'<span class="lf"><b>' + json.data.list[i].name + '</b>-<b>' + json.data.list[i].currentLevel + '</b></span>' +
								'<span class="rf mana_time">' + json.data.list[i].createTime + '</span>' +
								'</div><div class="h_bottom">' +
								'<span class="lf"><b>对接人</b> : <b>' + json.data.list[i].docker + '</b></span>' +
								'<span class="rf tel rejectthis">'+json.data.list[i].ApprovalStatus+'</span>' +
								'</div></div></div>';
								}
						}
					}
					$("#willpend").append(html);
					var iscroll = require("iscroll");
					common.initIscroll({
						container: "iscrollContent_pending",
						showScrollBar: true,
						pullUpActionCallBack: pending.pullUpActionCallBack,
						pullDownActionCallBack: pending.pullDownActionCallBack,
						loadAll: pending.loadedAllFlag
					});
					$(".tiaozhuan").unbind().on("click", function() {
						window.location.href = "#/index/pending_details";
						var pk_member = $(this).attr("pk_member").trim();
						window.localStorage.setItem("pk_member", pk_member);
						var listtype =$(this).attr("listtype"); 
						window.localStorage.setItem("listtype", listtype);
						window.localStorage.setItem("supertype", pending.supertype);
					})
					
				} else {
					if(pending.currentPage == 1) {
						common.show_nodata("当前没有会员消息", "willpend");
						common.loadedAll();
						pending.loadedAllFlag = true;
					} else {
						//两个部分共同控制加载全部
//						common.show_nodata("当前没有会员消息", "willpend");
						common.loadedAll();
						pending.loadedAllFlag = true;
					}
				}

			} else {
				common.showTip({
					content: json.message
				});
				if(pending.currentPage == 1) {
						common.show_nodata("当前没有会员消息", "willpend");
					} else {
						//两个部分共同控制加载全部
//						common.show_nodata("当前没有会员消息", "willpend");
						common.loadedAll();
						pending.loadedAllFlag = true;
					}
			}
		},
		pullUpActionCallBack: function() {
			pending.currentPage += 1;
			pending.loadData(pending.currentPage);
		},
		pullDownActionCallBack: function() {
			pending.currentPage = 1;
			//继续加载页面
			$("#willpend").html("");

			pending.loadedAllFlag = false;
			//common.continueLoad();
			pending.loadData(pending.currentPage);
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {

			this.loadData(this.currentPage = 1);
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = pending;
});